//index.js
//获取应用实例
var app = getApp()
Page({
  data: {
    advice: '',
    phone: ''
  },
  //事件处理函数
  submitAdvice: function (e) {
    var that = this;
    wx.request({
      url: app.globalData.base_url +"Question/addQuestion",
      header: {
        'Content-Type': 'application/json;charset=UTF-8;'
      },
      method: "POST",
      data: {
        "CUSTOMER_ID": wx.getStorageSync('user_id'),
        "QUESTION_CONTENT": that.data.advice,
        "CONTACT_NUMBER": that.data.phone
      },
      success: function (res) {
        if (res.data.code==="200") {

          wx.showToast({
            title: '提交成功',
            icon: 'success',
            duration: 2000
          })
          that.setData({
            advice: '',
            phone: ''
          });
        } else {
          wx.showToast({
            title: '提交失败',
            icon: 'loading',
            duration: 1500
          })
        }
       
      }
    })
  },
  onLoad: function () {
    wx.showShareMenu({
      withShareTicket: true
    })
  },

  //分享
  onShareAppMessage: function (res) {
    let that = this
    return {
      title: '找苗木网',
      path: '/pages/login1/login1',
      imageUrl: '../../source/icon/banner.png',
      success: function (res) {
        console.log(res.shareTickets)
        //getSystemInfo是为了获取当前设备信息，判断是android还是ios，如果是android
        //还需要调用wx.getShareInfo()，只有当成功回调才是转发群，ios就只需判断shareTickets
        //获取用户设备信息
        wx.getSystemInfo({
          success: function (d) {
            console.log(d);
            if (res.shareTickets != undefined) {
              console.log("分享的是群");
              wx.getShareInfo({
                shareTicket: res.shareTickets[0],
                success: function (res) {
                  //分享到群之后你要做的事情
                  wx.request({
                    url: app.globalData.base_url + "Customer/grantTicketByGroup",
                    header: {
                      'Content-Type': 'application/json;charset=UTF-8;'
                    },
                    method: "POST",
                    data: {
                      "CUSTOMER_ID": wx.getStorageSync('user_id'),
                    },
                    complete: function (res) {
                      that.setData({

                      });
                      if (res == null || res.data == null) {
                        console.error('网络请求失败');
                        return;
                      }
                    },
                    success: function (res) {
                      if (res.data.code === "200") {
                        wx.showModal({
                          title: '提示',
                          content: '赠送您一张估价券哦，有效时间24小时',
                          success: function (res) {
                            if (res.confirm) {
                              console.log('用户点击确定')
                            } else if (res.cancel) {
                              console.log('用户点击取消')
                            }
                          }
                        })
                      } else if (res.data.code === "203") {
                        var maxGrantCount = res.data.data;
                        wx.showModal({
                          title: '提示',
                          content: '分享到群每天最多赠送 ' + maxGrantCount + ' 张您已达到上限',
                          success: function (res) {
                            if (res.confirm) {
                              console.log('用户点击确定')
                            } else if (res.cancel) {
                              console.log('用户点击取消')
                            }
                          }
                        })
                      }
                    }
                  })
                }
              })
            } else {//分享到个人要做的事情，我给的是一个提示
              console.log("分享的是个人");
              wx.showModal({
                title: '提示',
                content: '分享到群赠送估价券哦',
                success: function (res) {
                  if (res.confirm) {
                    console.log('用户点击确定')
                  } else if (res.cancel) {
                    console.log('用户点击取消')
                  }
                }
              })
            }
          },
          fail: function (res) {

          }
        })
      }

    }
  },


  inputAdvice: function (e) {
    this.setData({
      advice: e.detail.value
    });
  },
  inputPhone: function (e) {
    this.setData({
      phone: e.detail.value
    });
  },
})
